﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sale.DTO.ImportExport;

namespace Sale.DataAccess.Repository
{
    public class ExportRepository : BaseRepository
    {
        private MerchandiseRepository _merRepo;

        public ExportRepository()
        {
            _merRepo = new MerchandiseRepository();
        }

        public IEnumerable<ExportDTO> GetExportList(DateTime date)
        {
            return Context.Exports.Where(e => e.ExportDate.Date == date.Date)
                .Select(e => ConvertToDTO(e))
                .AsEnumerable();
        }

        public IEnumerable<ExportDTO> GetExportList(DateTime from, DateTime to)
        {
            return Context.Exports.Where(e => e.ExportDate.Date >= from.Date
                && e.ExportDate.Date <= to.Date)
                .Select(e => ConvertToDTO(e))
                .AsEnumerable();
        }

        public IEnumerable<ExportDTO> GetExportListAfter(DateTime time)
        {
            return Context.Exports.Where(e => e.ExportDate >= time)
               .Select(e => ConvertToDTO(e));
        }
        public void AddExportItem(ExportDTO dto)
        {
            var entity = ConvertToEntity(dto);
            Context.Exports.InsertOnSubmit(entity);


        }

        public double GetTotalExportQuantity(DateTime date, int merchandiseId)
        {
            var list = Context.Exports.Where(
                e => e.ExportDate.Date == date.Date
                && e.MerchandiseId == merchandiseId);
            if (list.Count() > 0)
                return list.Sum(e => e.Quantity);
            return 0;
        }

        public decimal GetAvgExportUnitPrice(DateTime date, int merchandiseId)
        {
            var result = Context.Exports.Where(
                e => e.ExportDate.Date == date.Date
                && e.MerchandiseId == merchandiseId
                );
            if (result.Count() > 0)
                return result.Average(e => e.ExportPrice);
            return 0;
        }

        private ExportDTO ConvertToDTO(Export entity)
        {
            var dto = new ExportDTO()
            {
                Id = entity.Id,
                CustomerName = entity.Customer == null ? string.Empty : entity.Customer.CustomerName,
                CustomerId = entity.CustomerId,
                Merchandise = _merRepo.GetMerchandiseById(entity.MerchandiseId),
                ExportDate = entity.ExportDate,
                ExportPrice = entity.ExportPrice,
                Notes = entity.Notes,
                Quantity = entity.Quantity,
                CreatedBy = entity.CreatedBy,
                IsCancelled = entity.IsCancelled,
                OriginalPrice = entity.OriginalPrice
            };
            return dto;
        }

        private Export ConvertToEntity(ExportDTO dto)
        {
            var entity = new Export()
            {
                Id = dto.Id,
                CustomerId = dto.CustomerId,
                MerchandiseId = dto.Merchandise.Id,
                ExportDate = dto.ExportDate,
                ExportPrice = dto.ExportPrice,
                Notes = dto.Notes,
                Quantity = dto.Quantity,
                CreatedBy = dto.CreatedBy,
                IsCancelled = dto.IsCancelled,
                OriginalPrice = dto.OriginalPrice
            };
            return entity;
        }
    }
}
